﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================

using System.Data;
using System.Configuration;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using EntLibContrib.Logging.Database.Configuration.Unity;

namespace EntLibContrib.Logging.Database.Configuration
{
	/// <summary>
	/// Configuration object for a <see cref="ExtendedDatabaseTraceListener"/>.
	/// </summary>
	[Assembler(typeof(ExtendedDatabaseTraceListenerAssembler))]
	[ContainerPolicyCreator(typeof(ExtendedDatabaseTraceListenerPolicyCreator))]
	public class ExtendedDatabaseTraceListenerData : TraceListenerData
	{
		#region Fields
		private const string writeLogSqlCommandProperty = "writeLogSqlCommand";
		private const string addCategorySqlCommandProperty = "addCategorySqlCommand";
		private const string commandTypeProperty = "commandType";
		private const string databaseInstanceNameProperty = "databaseInstanceName";
		private const string formatterNameProperty = "formatter";
		#endregion

		#region Properties
		/// <summary>
		/// Gets and sets the database instance name.
		/// </summary>
		[ConfigurationProperty(databaseInstanceNameProperty, IsRequired = false)]
		public string DatabaseInstanceName
		{
			get { return (string)base[databaseInstanceNameProperty]; }
			set { base[databaseInstanceNameProperty] = value; }
		}

		/// <summary>
		/// Gets and sets the sql command for writing the log.
		/// </summary>
		[ConfigurationProperty(writeLogSqlCommandProperty, IsRequired = true)]
		public string WriteLogSqlCommand
		{
			get { return (string)base[writeLogSqlCommandProperty]; }
			set { base[writeLogSqlCommandProperty] = value; }
		}

		/// <summary>
		/// Gets and sets the sql command for adding a category for this log.
		/// </summary>
		[ConfigurationProperty(addCategorySqlCommandProperty, IsRequired = true)]
		public string AddCategorySqlCommand
		{
			get { return (string)base[addCategorySqlCommandProperty]; }
			set { base[addCategorySqlCommandProperty] = value; }
		}

		/// <summary>
		/// Gets and sets the command type for both sql commands.
		/// </summary>
		[ConfigurationProperty(commandTypeProperty, IsRequired = true)]
		public CommandType CommandType
		{
			get { return (CommandType)base[commandTypeProperty]; }
			set { base[commandTypeProperty] = value; }
		}

		/// <summary>
		/// Gets and sets the formatter name.
		/// </summary>
		[ConfigurationProperty(formatterNameProperty, IsRequired = false)]
		public string Formatter
		{
			get { return (string)base[formatterNameProperty]; }
			set { base[formatterNameProperty] = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a <see cref="ExtendedDatabaseTraceListenerData"/>.
		/// </summary>
		public ExtendedDatabaseTraceListenerData()
		{
		}

		/// <summary>
		/// Initializes a named instance of <see cref="ExtendedDatabaseTraceListenerData"/> with
		/// name, stored procedure name, databse instance name, and formatter name.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="writeLogSqlCommand">The stored procedure name for writing the log.</param>
		/// <param name="addCategorySqlCommand">The stored procedure name for adding a category for this log.</param>
		/// <param name="commandType">The command type.</param>
		/// <param name="databaseInstanceName">The database instance name.</param>
		/// <param name="formatterName">The formatter name.</param>
		public ExtendedDatabaseTraceListenerData(string name,
																						string writeLogSqlCommand,
																						string addCategorySqlCommand,
																						CommandType commandType,
																						string databaseInstanceName,
																						string formatterName)
			: this(
					name,
					writeLogSqlCommand,
					addCategorySqlCommand,
					commandType,
					databaseInstanceName,
					formatterName,
					TraceOptions.None,
					SourceLevels.All)
		{
		}

		/// <summary>
		/// Initializes a named instance of <see cref="ExtendedDatabaseTraceListenerData"/> with 
		/// name, stored procedure name, databse instance name, and formatter name.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="writeLogSqlCommand">The stored procedure name for writing the log.</param>
		/// <param name="addCategorySqlCommand">The stored procedure name for adding a category for this log.</param>
		/// <param name="databaseInstanceName">The database instance name.</param>
		/// <param name="formatterName">The formatter name.</param>
		/// <param name="traceOutputOptions">The trace options.</param>
		/// <param name="filter">The filter to be applied</param>
		public ExtendedDatabaseTraceListenerData(string name,
																						string writeLogSqlCommand,
																						string addCategorySqlCommand,
																						CommandType commandType,
																						string databaseInstanceName,
																						string formatterName,
																						TraceOptions traceOutputOptions,
																						SourceLevels filter)
			: base(name, typeof(ExtendedDatabaseTraceListener), traceOutputOptions, filter)
		{
			this.DatabaseInstanceName = databaseInstanceName;
			this.WriteLogSqlCommand = writeLogSqlCommand;
			this.AddCategorySqlCommand = addCategorySqlCommand;
			this.CommandType = commandType;
			this.Formatter = formatterName;
		}
		#endregion
	}
}
